from PIL import Image
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter

writer = SummaryWriter("../logs")

img_path = "data/train/ants_image/0013035.jpg"
img = Image.open(img_path)

# TOTensor
trans_totensor = transforms.ToTensor()
tensor_img = trans_totensor(img)
writer.add_image("ToTensor", tensor_img)

# Normalize:归一化
print(tensor_img[0][0][0])
trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_norm = trans_norm(tensor_img)
print(img_norm[0][0][0])
writer.add_image("norm", img_norm)

# Resize
trans_resize = transforms.Resize((512, 512))
# PIL -> resize -> PIL
img_resize = trans_resize(img)
img_resize = trans_totensor(img_resize)
writer.add_image("resie", img_resize)

# compose: 按比例缩放
trans_resize_2 = transforms.Resize(512)
# PIL -> PIL -> tensor
trans_compose = transforms.Compose([trans_resize_2, trans_totensor])
img_resize_2 = trans_compose(img)
writer.add_image("resize_2", img_resize_2)
writer.close()
